package wechat.app.corp.domain;

import com.google.common.base.Objects;
import java.util.Date;
import java.util.function.Consumer;
import org.apache.log4j.Logger;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.Pure;
import wechat.app.WCCorpApplication;
import wechat.base.snd.WCNews;
import wechat.base.snd.WCNewsItem;
import wechat.base.snd.util.TimeFormatter;

@SuppressWarnings("all")
public class EngineerNotificationCorpWeChat {
  private static Logger logger = Logger.getLogger("EngineerNotificationCorpWeChat");
  
  private static String CALLBACK_SERVER_URL = "http://weixin.shrct.com:8090";
  
  @Accessors
  private WCCorpApplication app;
  
  @Accessors
  private String cbkurl;
  
  public static EngineerNotificationCorpWeChat getIns() {
    return new EngineerNotificationCorpWeChat();
  }
  
  public String generateNotificationDetailsUrl(final String id) {
    return ((this.cbkurl + "/notificationdetail?notificationId=") + id);
  }
  
  public void sendNormalNotification(final Iterable<String> users, final String title, final String body, final String id) {
    final Consumer<String> _function = new Consumer<String>() {
      public void accept(final String it) {
        InputOutput.<String>println(("==========>send to " + it));
      }
    };
    users.forEach(_function);
    if (((IterableExtensions.size(users) == 0) || (!IterableExtensions.<String>exists(users, new Function1<String, Boolean>() {
      public Boolean apply(final String it) {
        return Boolean.valueOf((!Objects.equal(it, null)));
      }
    })))) {
      return;
    }
    WCNews _createNews = this.app.createNews();
    final Procedure1<WCNews> _function_1 = new Procedure1<WCNews>() {
      public void apply(final WCNews it) {
        WCNewsItem _withItem = it.withItem(title);
        final Procedure1<WCNewsItem> _function = new Procedure1<WCNewsItem>() {
          public void apply(final WCNewsItem it) {
            it.setUrl(EngineerNotificationCorpWeChat.this.generateNotificationDetailsUrl(id));
          }
        };
        ObjectExtensions.<WCNewsItem>operator_doubleArrow(_withItem, _function);
        WCNewsItem _withItem_1 = it.withItem(body);
        final Procedure1<WCNewsItem> _function_1 = new Procedure1<WCNewsItem>() {
          public void apply(final WCNewsItem it) {
            it.setUrl(EngineerNotificationCorpWeChat.this.generateNotificationDetailsUrl(id));
          }
        };
        ObjectExtensions.<WCNewsItem>operator_doubleArrow(_withItem_1, _function_1);
        it.send2Users(users);
      }
    };
    ObjectExtensions.<WCNews>operator_doubleArrow(_createNews, _function_1);
  }
  
  public void sendNewAssignmentEvent(final String wechatid, final String assignmentId, final String projectName, final Date starttime, final Date endtime) {
    final String link = (((this.cbkurl + "/assignmentdetail?assignmentId=") + assignmentId) + "&state=newtask");
    WCNews _createNews = this.app.createNews();
    final Procedure1<WCNews> _function = new Procedure1<WCNews>() {
      public void apply(final WCNews it) {
        WCNewsItem _withItem = it.withItem("您有任务未确认");
        final Procedure1<WCNewsItem> _function = new Procedure1<WCNewsItem>() {
          public void apply(final WCNewsItem it) {
            it.setUrl(link);
          }
        };
        ObjectExtensions.<WCNewsItem>operator_doubleArrow(_withItem, _function);
        String _timeFormat = TimeFormatter.timeFormat(starttime);
        String _plus = ((("项目名称：" + projectName) + "\n任务开始时间：") + _timeFormat);
        String _plus_1 = (_plus + "\n任务结束时间：");
        String _timeFormat_1 = TimeFormatter.timeFormat(endtime);
        String _plus_2 = (_plus_1 + _timeFormat_1);
        WCNewsItem _withItem_1 = it.withItem(_plus_2);
        final Procedure1<WCNewsItem> _function_1 = new Procedure1<WCNewsItem>() {
          public void apply(final WCNewsItem it) {
            it.setUrl(link);
          }
        };
        ObjectExtensions.<WCNewsItem>operator_doubleArrow(_withItem_1, _function_1);
        it.send2Users(CollectionLiterals.<String>newArrayList(wechatid));
      }
    };
    ObjectExtensions.<WCNews>operator_doubleArrow(_createNews, _function);
  }
  
  public void sendAssignmentAcceptEvent(final String wechatid, final String assignmentId, final String projectName, final String engineer, final Date starttime, final Date endtime) {
    WCNews _createNews = this.app.createNews();
    final Procedure1<WCNews> _function = new Procedure1<WCNews>() {
      public void apply(final WCNews it) {
        WCNewsItem _withItem = it.withItem("任务还未被接受");
        final Procedure1<WCNewsItem> _function = new Procedure1<WCNewsItem>() {
          public void apply(final WCNewsItem it) {
          }
        };
        ObjectExtensions.<WCNewsItem>operator_doubleArrow(_withItem, _function);
        String _timeFormat = TimeFormatter.timeFormat(starttime);
        String _plus = ((((("项目名称：" + projectName) + "\n工程师：") + engineer) + "\n任务开始时间：") + _timeFormat);
        String _plus_1 = (_plus + "\n任务结束时间：");
        String _timeFormat_1 = TimeFormatter.timeFormat(endtime);
        String _plus_2 = (_plus_1 + _timeFormat_1);
        WCNewsItem _withItem_1 = it.withItem(_plus_2);
        final Procedure1<WCNewsItem> _function_1 = new Procedure1<WCNewsItem>() {
          public void apply(final WCNewsItem it) {
          }
        };
        ObjectExtensions.<WCNewsItem>operator_doubleArrow(_withItem_1, _function_1);
        it.send2Users(CollectionLiterals.<String>newArrayList(wechatid));
      }
    };
    ObjectExtensions.<WCNews>operator_doubleArrow(_createNews, _function);
  }
  
  @Pure
  public WCCorpApplication getApp() {
    return this.app;
  }
  
  public void setApp(final WCCorpApplication app) {
    this.app = app;
  }
  
  @Pure
  public String getCbkurl() {
    return this.cbkurl;
  }
  
  public void setCbkurl(final String cbkurl) {
    this.cbkurl = cbkurl;
  }
}
